import { JassParser, LibraryDefinition } from "./jassParser";
import * as fs from 'fs';

const parser = new JassParser();

async function genDefs() {
    await parser.main(['', '', "jass/blizzard.j", "types/war3/blizzard.d.ts"]);
    await parser.main(['', '', "jass/common.j", "types/war3/common.d.ts"]);
    await parser.main(['', '', "jass/common.ai", "types/war3/commonai.d.ts"]);

    await parser.main(['', '', "jass/japi.j", "types/japi.d.ts"]);
    await parser.main(['', '', "jass/old.j", "types/old.d.ts"]);
    await parser.main(['', '', "jass/rest.j", "types/rest.d.ts"]);

    await parser.main(['', '', "jass/rest.j", "types/rest.d.ts"]);

    await parser.main(['', '', "back/blizzard.j", "ydluatype/war3/blizzard.d.ts"]);
    await parser.main(['', '', "back/common.j", "ydluatype/war3/common.d.ts"]);
    await parser.main(['', '', "back/DzAPI.j", "ydluatype/war3/DzAPI.d.ts"]);
    await parser.main(['', '', "back/japi.j", "ydluatype/war3/japi.d.ts"]);

}

// genDefs();

function parseBzLua(path: string) {
    //读取lua分解每一行
    let con = fs.readFileSync(path, 'utf8').split('\n')
    let str = "_C = {} \r";
    let tsStr = "";
    con.forEach((e) => {
        if (e[0] != '\r' && e[0] != "-") {
            let t = e.split("--");
            let temp = `_C.${t[0].trim()}\r`;
            str = str + temp;
            // 拆分等于号
            let defineArray = t[0].replace('nil', 'null').split("=")
            tsStr += `export const  ${defineArray[0].trim()} = ${defineArray[1].trim()}  \r\n`;
        }
    })
    fs.writeFileSync("./lua/C.lua", str)
    fs.writeFileSync("./lua/C.ts", tsStr)
}


parseBzLua("./lua/blizzard.lua");
// 所有api
import * as api from "./apis";
let apiArray = api.apis;


function parseCLua(path: string) {
    //读取lua分解每一行
    let con = fs.readFileSync(path, 'utf8').split('\n')

    let str = "_F = {} \r";

    con.forEach((e) => {
        let t = e.split("=");
        let f = t[0].trim()
        let fun = apiArray.find(e => e.nameUS == f);
        // console.log("fun",fun);
        let temp = `\r`;

        if (fun) {
            //合并函数
            if (fun.title) {
                temp += "---" + fun.title + `\r`;
            }
            if (fun.description) {
                temp += "---" + fun.description + `\r`;
            }
            if (fun.comment) {
                temp += "---" + fun.comment + `\r`;
            }
            let typeStr = "---@type fun("
            if (fun.args) {
                let args = fun.args.split(",");

                args.forEach((a, i) => {
                    let at = a.trim().split(" ");
                    if (at[0] != "nothing") {
                        typeStr += `${at[1]}:${at[0]},`
                    }
                })
                typeStr += "):"
            }
            typeStr += fun.returns + `\r`;
            temp += typeStr;

            temp += `_F.${fun.nameUS} = jc["${fun.nameUS}"]\r`

        } else if (f) {
            let typeStr = "---@type fun()\r"
            temp += typeStr;
            temp += `_F.${f} = jc["${f}"]\r`
        }
        if (temp) {
            str = str + temp;
        }
    })

    fs.writeFileSync("./lua/F.lua", str)
}

// parseCLua("./lua/engine.lua");